package org.lmw.mm.dao;

import com.jfinal.plugin.activerecord.Model;

/**
 * 
 * 此类描述的是：
 * 
 * @author: limengwei
 * @version: 2014-6-5 上午11:47:58
 */
public class Like_Post extends Model<Like_Post> {
  private static final long serialVersionUID = -8015021741160292107L;
  static String code_error = "0";
  static String code_success = "1";
  public static final Like_Post dao = new Like_Post();

  /**
   * 
   * 此方法描述的是：点赞
   * 
   * @author: limengwei
   * @version: 2014-6-6 上午10:54:34
   */
  public String like(int postId, int userId) {
    Like_Post lp = dao.findFirst("select * from mm_like_post where postId=? and userId=?", postId, userId);
    if (lp == null) {
      if (new Like_Post().set("postId", postId).set("userId", userId).set("liked", 1).save()) {
         if(Post.dao.like(postId)){
           return code_success; 
         }
      }
    } else {
      if (lp.set("liked", 1).update()) {
        if(Post.dao.like(postId)){
          return code_success; 
        }
      }
    }
    return code_error;
  }

  /**
   * 
   * 此方法描述的是：取消赞
   * 
   * @author: limengwei
   * @version: 2014-6-6 上午10:55:30
   */
  public String unLike(int postId, int userId) {
    Like_Post lp = dao.findFirst("select * from mm_like_post where postId=? and userId=?", postId, userId);
    if (lp.set("liked", 0).update()) {
      if(Post.dao.unLike(postId)){
        return code_success; 
      }
    } 
      return code_error;
  }
}
